from datetime import datetime
from typing import List, Optional

from pydantic import BaseModel, EmailStr, Field

from app.models import StatusType


class BaseUser(BaseModel):
    id: int
    email: Optional[EmailStr] = None
    username: Optional[str] = None
    alias: Optional[str] = None
    phone: Optional[str] = None
    is_superuser: Optional[bool] = False
    created_at: Optional[datetime]
    updated_at: Optional[datetime]
    last_login: Optional[datetime]
    roles: Optional[list] = []


class UserCreate(BaseModel):
    email: EmailStr = Field(examples=["admin@qq.com"])
    username: str = Field(examples=["admin"])
    alias: str = Field(description="姓名", examples=["张三"])
    phone: str = Field(description="手机号", examples=["13333333333"])
    password: str = Field(examples=["123456"])
    status: Optional[int] = Field(StatusType.ENABLED, description="状态")
    is_superuser: Optional[bool] = False
    role_ids: Optional[List[int]] = []
    dept_id: Optional[int] = Field(0, description="部门ID")

    def create_dict(self):
        return self.model_dump(exclude_unset=True, exclude={"role_ids"})


class UserUpdate(BaseModel):
    id: int
    email: EmailStr
    username: str
    alias: str = Field(description="姓名", examples=["张三"])
    phone: str = Field(description="手机号", examples=["13333333333"])
    status: Optional[int] = Field(None, description="状态")
    is_superuser: Optional[bool] = False
    role_ids: Optional[List[int]] = []
    dept_id: Optional[int] = 0


class UpdatePassword(BaseModel):
    old_password: str = Field(description="旧密码")
    new_password: str = Field(description="新密码")
